package com.cbd.controller;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cbd.pojo.Complaintrecord;
import com.cbd.pojo.Personalcarport;
import com.cbd.pojo.Purpose;
import com.cbd.pojo.Record;
import com.cbd.pojo.RentInfo;
import com.cbd.pojo.Renter;
import com.cbd.pojo.ShowRenter;
import com.cbd.pojo.User;
import com.cbd.service.RenterService;
import com.cbd.service.UserService;
import com.cbd.util.IQuantity;

@Controller
public class RenterController{
	@Resource(name="renterServiceImpl")
	private RenterService renterService;
	@Resource(name="userServiceImpl")
	private UserService userService;
	
	public void setRenterService(RenterService renterService) {
		this.renterService = renterService;
	}
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	@RequestMapping("renterRegist")
	public String renterRegist(Renter renter){
		try {
			renterService.regist(renter);
			return "redirect:login.jsp";
		} catch (Exception e) {
			// TODO: handle exception
		}
		return "error.jsp";
		
	} 
	@RequestMapping("/cbd/renter/findRenterByUserID")
	public @ResponseBody Renter findRenterByUserID(HttpServletRequest request){
		int userID = Integer.parseInt(request.getParameter("userID"));
		Renter renter = renterService.findRenterByUserID(userID);
		renter.setPurposes(null);
		return renter;
	}
	@RequestMapping("/cbd/renter/updateRenter")
	public String updateRenter(@Valid Renter renter,BindingResult br,
			HttpServletRequest request){
		if(br.hasErrors()){
			System.out.println("there hava errors");
			return "forward:updateRenter.jsp";
		}
		int userID = Integer.parseInt(request.getParameter("userID"));
		//这样得到的user中只会包含userName 其余的属性都为null
		User user = renter.getUser();
//		String userPWD = request.getParameter("user.userPWD");
		//通过方法取得user   直接在参数中写User user  在后台验证时会出现  客户端发送的请求有语法错误
		//错误信息：The request sent by the client was syntactically incorrect
//		User user = userService.findUserByUserID(userID);
//		user.setUserPWD(userPWD);
		user.setUserID(userID);
		renter.setUser(user);		
		int result = renterService.updateRenterByRenter(renter);
		return "redirect:welcome.jsp";
	}
	@RequestMapping("/cbd/renter/findOnePageRecord")
	public @ResponseBody List<Object []> findOnePageRecord(HttpServletRequest request){
		int userID = Integer.parseInt(request.getParameter("userID"));
		int currentPage = Integer.parseInt(request.getParameter("currentPage"));
		//通过后台方法找到需要的所有参数的list
		List<Object []> records = renterService.findOnePageRecordByUserID(userID, IQuantity.pageSize, currentPage);
		return records;
	}
	@RequestMapping("/cbd/renter/countRecordPageCount")
	public @ResponseBody int countRecordPageCount(HttpServletRequest request){
		int userID = Integer.parseInt(request.getParameter("userID"));
		int pageSize = IQuantity.pageSize;
		//通过后台方法获取到所有的list
		List<Object []> list = renterService.findAllRecordByUserID(userID);
		int pageCount = list.size()%pageSize == 0 ? list.size()/pageSize : (list.size()/pageSize +1);
		return pageCount;
	}

	
	
	@RequestMapping(path="showRenters" ) 
	public @ResponseBody List<ShowRenter> showRenter(HttpServletRequest req){
		String page=req.getParameter("page");
		//System.out.println("page"+req.getParameter("page"));
		int page1=Integer.parseInt(page+"");
		System.out.println(page1);
		List<ShowRenter> list=renterService.showRenter(page1);
		return list;
		
	}
	@RequestMapping("/cbd/renter/findRecordById")
	public @ResponseBody Object[] findRecordById(HttpServletRequest request){
		int recordID = Integer.parseInt(request.getParameter("recordID"));
		Object[] objects = renterService.findRecordByRecordID(recordID);
		return objects;
	}
	@RequestMapping(path="/cbd/renter/addComplaintRecord",method=RequestMethod.POST)
	public String addComplaintRecord(@Valid Complaintrecord complaintrecord,BindingResult br,
			HttpServletRequest request){
		int recordID = Integer.parseInt(request.getParameter("recordID"));
		if(br.hasErrors()){
			return "forward:complaintrecord.jsp";
		}
		Record record = new Record();
		record.setRecordID(recordID);
		complaintrecord.setRecord(record);
		complaintrecord.setTime(new Date());
		complaintrecord.setType(false);
		complaintrecord.setComplaint(false);
		//实现方法  增加一条投诉记录  
		int result = renterService.addComplaintrecord(complaintrecord);
		return "redirect:rentRecord.jsp";
	}
	@RequestMapping("/cbd/renter/countCarPortPageCount")
	public @ResponseBody int countCarPortPageCount(HttpServletRequest request){
		int pageSize = IQuantity.pageSize;
		String startTime = request.getParameter("startTime");
		String endTime = request.getParameter("endTime");
		int userID = Integer.parseInt(request.getParameter("userID"));
		List<Object[]> carPorts = renterService.findCarPortsByTimes(startTime,endTime,userID);
		int pageCount = carPorts.size()%pageSize == 0 ? carPorts.size()/pageSize 
					: carPorts.size()/pageSize +1;
		return pageCount;
	}
	@RequestMapping("/cbd/renter/findOnePageCarPort")
	public @ResponseBody List<Object[]> findOnePageCarPort(HttpServletRequest request){
		String startTime = request.getParameter("startTime");
		String endTime = request.getParameter("endTime");
		int userID = Integer.parseInt(request.getParameter("userID"));
		int currentPage = Integer.parseInt(request.getParameter("currentPage"));
		List<Object[]> carPorts = renterService.findOnePageCarPortByTime(currentPage,startTime,endTime,userID);
		return carPorts;
	}
	@RequestMapping("/cbd/renter/addPurpose")
	public String addPurpose(@Valid Purpose purpose,BindingResult brResult,HttpServletRequest request){
		int userID = Integer.parseInt(request.getParameter("userID"));
		int rentInfoID = Integer.parseInt(request.getParameter("rentInfoID"));
		if(brResult.hasErrors()){
			return "forward:rentCarPort.jsp";
		}
		//通过userID得到renter
		Renter renter = renterService.findRenterByUserID(userID);
		purpose.setRenter(renter);
		//通过rentInfoID得到rentInfo
		RentInfo rentInfo = renterService.findRentInfoByRentInfoID(rentInfoID);
		purpose.setRentInfo(rentInfo);
		//增加一条 意向purpose
		int result = renterService.addPurpose(purpose);
		return "forward:rentCarPort.jsp";
	}
	@RequestMapping("/cbd/renter/checkRecordIDInTable")
	public @ResponseBody int checkRecordIDInTable(HttpServletRequest request){
		int recordID = Integer.parseInt(request.getParameter("recordID"));
		return renterService.findRecordIDFromComplaintByRecordID(recordID);
	}
}
